package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import io.renren.modules.sys.config.SysBottomConfig;
import io.renren.modules.sys.entity.SysOddsCatetoryEntity;
import io.renren.modules.sys.service.SysBottomUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysBottomEntity;
import io.renren.modules.sys.service.SysBottomService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 开奖号码记录表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-11-08 19:36:41
 */
@RestController
@RequestMapping("/sys/sysbottom")
public class SysBottomController extends AbstractController{

	@Autowired
	private SysBottomConfig sysMainConfig;

	@Autowired
	private SysBottomService sysBottomService;

	@Autowired
	private SysBottomUserService sysBottomUserService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:sysbottom:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysBottomEntity> sysBottomList = sysBottomService.queryList(query);
		int total = sysBottomService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysBottomList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{bottomId}")
	@RequiresPermissions("sys:sysbottom:info")
	public R info(@PathVariable("bottomId") Long bottomId){
		SysBottomEntity sysBottom = sysBottomService.queryObject(bottomId);
		
		return R.ok().put("sysBottom", sysBottom);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:sysbottom:save")
	public R save(@RequestBody SysBottomEntity sysBottom){
//		sysBottomService.save(sysBottom);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:sysbottom:update")
	public R update(@RequestBody SysBottomEntity sysBottom){

		SysBottomEntity bottomEntity = sysBottomService.queryObject(sysBottom.getBottomId());
		if (bottomEntity == null) {
			return R.error("未查询到该条记录！");
		}
		if (StringUtils.isNotBlank(bottomEntity.getBottomNumber())) {
			return R.error("已开奖，无法修改");
		}
		if (StringUtils.isNotBlank(sysBottom.getBottomNumber())) {
			String winNumbers = sysBottom.getBottomNumber().replace("，", ",").trim();
			if (StringUtils.split(winNumbers, ",").length < 5) {
				return R.error("开奖号码输入有误！");
			}
			//中文逗号替换为英文逗号，并去除空格
			sysBottom.setBottomNumber(winNumbers);
		}
		sysBottom.setUpdatePerson(getUserId());

		sysBottomService.update(sysBottom);
		//第一次编辑中奖号码，发奖
		if (StringUtils.isBlank(bottomEntity.getBottomNumber())) {
			sysBottomUserService.lottery(sysBottom);
		}

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:sysbottom:delete")
	public R delete(@RequestBody Long[] bottomIds){
		sysBottomService.deleteBatch(bottomIds);
		
		return R.ok();
	}

	/**
	 * 获取玩法信息
	 */
	@RequestMapping("/info")
	public R info(){

		R r = R.ok();
		r.put("sumList", sysMainConfig.getSumList());
		r.put("ballCategorys", sysMainConfig.getBallCategorys());
		r.put("specials", sysMainConfig.getSpecials());
		r.put("currentBottom", sysBottomService.queryCurrent());

		return r;
	}

	@RequestMapping("/getPreBottom")
	public R getPreBottom() {

		R r = R.ok();

		Map<String, Object> param = Maps.newHashMap();
		param.put("offset", 0);
		param.put("limit", 1);
		param.put("bottomNumber", "IS_NOT_NULL");

		List<SysBottomEntity> bottomEntities = sysBottomService.queryList(param);
		if (CollectionUtils.isNotEmpty(bottomEntities)) {
			SysBottomEntity sysBottomEntity = bottomEntities.get(0);
			sysBottomEntity.setBottomNumber(sysBottomEntity.getBottomNumber().replace(",", " "));
			r.put("preBottom", bottomEntities.get(0));
		}
		return r;
	}

	@RequestMapping("/getCurrentBottom")
	public R getCurrentBottom() {

		R r = R.ok();

		SysBottomEntity sysBottomEntity = sysBottomService.queryCurrent();

		r.put("currentBottom", sysBottomEntity);

		return r;
	}
}
